﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sofire.Data;

namespace SofireXSqlEntityGenerator.Core
{
    public class MsSqlGenerator : SqlGeneratorBase
    {
        public MsSqlGenerator(QueryEngineBase engine) : base(engine) { }

        public override TableResult GetUserTables()
        {
            return this.Engine.ExecuteTable("SELECT NAME AS TABLE_NAME FROM SYSOBJECTS WHERE XTYPE='U' ORDER BY NAME");
        }

        public override TableResult GetUserViews()
        {
            return this.Engine.ExecuteTable("SELECT NAME AS TABLE_NAME FROM SYSOBJECTS WHERE XTYPE='V' ORDER BY NAME");
        }

        public override TableResult GetTableColumns(string tableName)
        {
            return this.Engine.ExecuteTable(@"
SELECT RTRIM(B.NAME) AS FiledName
       ,B.IsNullable 
       ,ISNULL(C.VALUE,'') AS FieldComments
FROM SYSOBJECTS A INNER JOIN  SYS.ALL_OBJECTS AA ON A.ID=AA.OBJECT_ID AND  SCHEMA_NAME(SCHEMA_ID)='DBO'
    ,SYSCOLUMNS B LEFT OUTER JOIN SYS.EXTENDED_PROPERTIES C ON B.ID = C.MAJOR_ID AND B.COLID = C.MINOR_ID
WHERE    (A.ID = B.ID)
         AND (A.ID = OBJECT_ID(@tableName))
ORDER BY B.COLID;", "@tableName", tableName);
        }
    }
}
